package avicit.bdp.dds.server.master.dispatch.host.assign;

import java.util.Collection;
import java.util.Random;

/**
 * random selector
 * @param <T> T
 */
public class RandomSelector<T> implements Selector<T> {

    private final Random random = new Random();

    @Override
    public T select(final Collection<T> source) {

        if (source == null || source.size() == 0) {
            throw new IllegalArgumentException("Empty source.");
        }

        /**
         * if only one , return directly
         */
        if (source.size() == 1) {
            return (T) source.toArray()[0];
        }

        int size = source.size();
        /**
         *  random select
         */
        int randomIndex = random.nextInt(size);

        return (T) source.toArray()[randomIndex];
    }

}
